home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: Alpha / Whiteline Alpha.iso / progtool / modula2 / lpr / realinou.def < prev    next >
Encoding:
Modula Definition  |  1994-09-22  |  5.7 KB  |  161 lines

  1. DEFINITION MODULE RealInOut;
  2.  
  3. (*      Real input output as per Wirth with extensions
  4.  
  5.         All items (except Done) exists in normal and long versions.*)
  6.  
  7.   VAR Done : BOOLEAN; 
  8.        (* TRUE if ReadReal or StringToReal returns correct number *)
  9.        
  10.   (* The two first PORCEDUREs don't normaly belong to RealInOut.
  11.      They provide means to overcome two serious problems using
  12.      the TYPE LONGREAL in this Modula-2, and are placed in this
  13.      MODULE only because it is most probably imported to all 
  14.      programs using LONGREALs.
  15.      Examples on how to use LONGREALs can be found in REALINOU.MOD,
  16.      LONGMATH.MOD, and TEST.MOD                                         *)
  17.  
  18.   PROCEDURE Rec(x : LONGREAL) : LONGREAL; 
  19.       (* Returns 1.0/x in full precision. Use this instead of 
  20.          long division if full double precision is needed 
  21.          with this Modula-2 !
  22.          The standard division '/' is only correct to about the 
  23.          10th significant digit, but faster then Rec *)
  24.  
  25.   PROCEDURE Long(a,b,c,d,e,exp : INTEGER) : LONGREAL;
  26.       (* Returns a LONGREAL composed of the six INTEGER arguments.
  27.          This should help to overcome some of the problems with 
  28.          non-existing CONSTants of LONGREAL type.
  29.          a, b, c, d, and e should all be positive constants with
  30.          four decimal digits.
  31.          A decimal point should be imagined between the 
  32.          two first digits of a. 
  33.          a, b, c, d, and e are concatenated to a long real number.
  34.          exp is the exponent to ten.
  35.          For example
  36.             LONG(1234,5678,9012,3456,7890,-123)
  37.                = 1.2345678901234567890E-123
  38.          StringToLongReal below provides an alternative to Long.
  39.          (LONG)CARDinals can be converted by FLOATD.
  40.          (LONG)INTegers can be converted by System.FLOATd.
  41.          REALs can be converted by System.FLONG or by assignment
  42.            (both with reduced precision)                               *)
  43.  
  44.   PROCEDURE ReadReal(VAR x : REAL);
  45.  
  46.   PROCEDURE ReadLongReal(VAR x : LONGREAL);
  47.  
  48.   PROCEDURE StringToReal
  49.          (VAR Text : ARRAY OF CHAR; VAR x : REAL;     VAR ReadOK : BOOLEAN);
  50.              
  51.   PROCEDURE StringToLongReal
  52.          (VAR Text : ARRAY OF CHAR; VAR x : LONGREAL; VAR ReadOK : BOOLEAN);
  53.  
  54.     (*      Convert a string to a real number
  55.  
  56.             Syntax is [+!-][d*][.d*][E[+!-]d*]
  57.  
  58.             eg      1.0
  59.                     2
  60.                     .3
  61.                     E3
  62.                     3.45E-17
  63.  
  64.             Termination character is returned in 
  65.             termCH from InOut (since this module is
  66.             effectively an extension of InOut)
  67.  
  68.             The StringToReal routines allow you to read a real number 
  69.             from a string. 
  70.             *)
  71.  
  72.   VAR
  73.   
  74.     Engineering     : BOOLEAN (* = FALSE *);
  75.     
  76.     LongEngineering : BOOLEAN (* = FALSE *); 
  77.  
  78.     (* if true, engineering exponents are used in WriteReal and 
  79.        RealToString. Engineering exponents are always multiples of 3 *)
  80.  
  81.     SigDigits     : INTEGER (* = 8 *);
  82.  
  83.     LongSigDigits : INTEGER (* = 16 *);
  84.     
  85.   (* number of significant digits in WriteReal and RealToString *)
  86.   
  87.     ForceExponent     : BOOLEAN (* = TRUE *); 
  88.  
  89.     LongForceExponent : BOOLEAN (* = TRUE *); 
  90.  
  91.     (* set to false to not output exponents of zero on floating point
  92.        numbers*)
  93.  
  94.   PROCEDURE WriteReal(x : REAL; size : INTEGER);
  95.  
  96.   PROCEDURE WriteLongReal(x : LONGREAL; size : INTEGER);
  97.  
  98.   PROCEDURE RealToString
  99.                   (VAR Text : ARRAY OF CHAR; x : REAL;     size : INTEGER);
  100.  
  101.   PROCEDURE LongRealToString
  102.                   (VAR Text : ARRAY OF CHAR; x : LONGREAL; size : INTEGER);
  103.  
  104. (*          Convert a real number to a string in mantissa + exponent form
  105.  
  106.             for example, 10 will be output as 1.0E+01  (Long: 1.0E+001).
  107.  
  108.             If Engineering is true, the number is scaled so that the 
  109.             mantissa is rounded to a multiple of 3 (the number of digits 
  110.             before the dot is increased).
  111.  
  112.             so, 10 will print as 10.0E+00   (Long: 10.0E+000).
  113.  
  114.             The layout is as follows:
  115.  
  116.             The rightmost 4 (Long: 5) columns of the field are reserved 
  117.             for the  exponent. There must then be SigDigits+2 character 
  118.             positions  left in the field (of size). If  insufficient 
  119.             positions  remain, extra columns are used for the number.
  120.  
  121.             RealToString returns the number as a zero terminated 
  122.             string in Text
  123. *)
  124.  
  125.   PROCEDURE WriteRealFixed(x : REAL; size, places : INTEGER);
  126.  
  127.   PROCEDURE WriteLongRealFixed(x : LONGREAL; size, places : INTEGER);
  128.  
  129.   PROCEDURE RealToStringFixed
  130.           (VAR Text : ARRAY OF CHAR; x : REAL;     size, places : INTEGER);
  131.  
  132.   PROCEDURE LongRealToStringFixed
  133.           (VAR Text : ARRAY OF CHAR; x : LONGREAL; size, places : INTEGER);
  134.  
  135. (*          Write a real number in fixed point.
  136.             size is the size of field to use.
  137.             places is the number of places for decimals.
  138.  
  139.             If the number is too large to fit in the field, 
  140.             floating point format is reverted to.
  141.         
  142.             If the number is too small, it prints as zero.
  143.  
  144.             RealToStringFixed returns the value in Text, as a zero 
  145.             terminated string.
  146. *)
  147.  
  148.   PROCEDURE WriteRealOct(x : REAL; n : CARDINAL);
  149.  
  150.   PROCEDURE WriteLongRealOct(x : LONGREAL; n : CARDINAL);
  151.  
  152.          (* output real number in octal for debugging purposes *)
  153.         
  154.   PROCEDURE WriteRealHex(x : REAL; n : CARDINAL);
  155.  
  156.   PROCEDURE WriteLongRealHex(x : LONGREAL; n : CARDINAL);
  157.  
  158.          (* output real number in hex for debugging purposes *)
  159.  
  160. END RealInOut. 
  161.